using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ZeroSum.App.Filters;
using ZeroSum.App.Middlewares;
using ZeroSum.DependencyInjection.Attributes;

namespace ZeroSum.App;

/// <summary>
///     AspNetCore层的初始化操作
/// </summary>
[StartupOrder(9700)]
public class Startup
{
    public void ConfigurationServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddHttpContextAccessor();

        // 返回数据统一格式化处理
        services.AddControllers().AddMvcOptions(options => { options.Filters.Add<ApiResponseFilter>(); });
    }

    public void Configuration(IApplicationBuilder app)
    {
        // 全局异常处理
        app.EnableExceptionHandler();

        // 自动提交数据库改动
        app.EnableAutoSave();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}